<template>
  <div>
    <!-- 搜索栏 -->
    <el-form v-show="showSearch" :inline="true" :model="queryParams">
      <el-form-item label="名称:">
        <el-input v-model="queryParams.name" clearable placeholder="请输入课程名"
                  style="width: 150px" @input="handleQuery"/>
      </el-form-item>
      <el-form-item label="学生:">
        <el-input v-model="queryParams.studentName" clearable placeholder="请输入学生姓名"
                  style="width: 150px" @input="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">
          <svg-icon icon-file-name="white-search" style="color: #1abc9c"></svg-icon>
          搜索
        </el-button>
      </el-form-item>
    </el-form>
    <!-- 操作按钮 -->
    <el-row :gutter="10" style="margin-bottom: 15px">
      <el-col :span="1.5">
        <el-button :disabled="studentSelectInfoIdList.length === 0" icon="Delete" plain type="danger"
                   @click="handleBatchDelete">批量删除
        </el-button>
      </el-col>
      <refresh-and-hide @refresh="handleRefresh" @toggle-search="handleToggleSearch"/>
    </el-row>
    <div class="scroll-container">
      <!-- 表格展示 -->
      <el-table v-loading="loading" :data="studentSelectInfoList" border @sort-change="handleSortChange"
                @selection-change="handleSelectionChange">
        <!-- 表格选择列 -->
        <el-table-column align="center" type="selection" width="55"></el-table-column>
        <el-table-column align="center" label="ID" prop="selectInfoId" width="55"></el-table-column>
        <!--缩略图-->
        <el-table-column align="center" label="课程封面" prop="courseImage" width="180">
          <template #default="scope">
            <el-image :lazy="true" :src="scope.row.courseImage" class="image-cover" fit="cover">
              <div slot="error" class="image-slot">
                <i class="el-icon-picture-outline"></i>
              </div>
            </el-image>
          </template>
        </el-table-column>
        <!-- 课程名 -->
        <el-table-column align="center" label="课程" prop="name" width="160"></el-table-column>
        <el-table-column align="center" label="任课老师" prop="teacherName" width="160"></el-table-column>
        <el-table-column align="center" label="学生" prop="studentName" width="90"></el-table-column>
        <el-table-column align="center" label="年级" prop="grade" width="90">
          <template #default="scope">
            {{ ['大一', '大二', '大三', '大四'][scope.row.grade - 1] }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="课程描述" prop="description"></el-table-column>
        <el-table-column align="center" label="课程学分" prop="credits" sortable="custom" width="160">
        </el-table-column>
        <el-table-column align="center" label="学生已修学分" prop="learnCredits" sortable="custom" width="160">
        </el-table-column>
        <el-table-column align="center" label="状态" prop="status">
          <!-- status状态[1:正常，0:已退课，2:已结课]-->
          <template #default="scope">
            {{ formatStatus(scope.row.status) }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="选课时间" prop="selectTime" sortable width="130">
          <template #default="scope">
            <div class="time">
              <svg-icon icon-file-name="time"></svg-icon>
              <span style="margin-left: 10px">{{ scope.row.selectTime }}</span>
            </div>
          </template>
        </el-table-column>
        <!-- 操作 -->
        <el-table-column align="center" label="操作" width="220">
          <template #default="scope">
            <el-button class="sd" link type="danger" @click="handleDelete(scope.row.selectInfoId)">
              <svg-icon icon-file-name="delete"></svg-icon>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页组件 -->
    <pagination v-if="total > 0" :limit="pagInfo.pageSize" :page="pagInfo.currentPage" :total="total"
                style="float: right;" @pagination="getStudentSelectInfoList"
                @update_limit="pagInfo.pageSize = $event"
                @update_page="pagInfo.currentPage = $event"/>

  </div>
</template>

<script>
import refreshAndHide from "@/common/refreshAndHide.vue";
import Pagination from "@/common/pagination.vue";
import {
  getStudentSelectRecordList,
  deleteStudentSelectInfo, deleteStudentSelectInfoOnList
} from "@/apis/admin";

export default {
  // 组件注册，允许在模板中使用导入的组件
  components: {
    Pagination, // 分页组件
    refreshAndHide // 刷新和隐藏组件
  },
  // 组件的响应数据对象
  data() {
    return {
      // 控制搜索表单显示的布尔值
      showSearch: true,
      // 搜索参数对象
      queryParams: {
        name: '', // 课程名
        studentName: '', // 任课老师姓名
      },
      //分页信息
      pagInfo: {
        currentPage: 1,
        pageSize: 10
      },
      // 存储选中的课程ID列表
      studentSelectInfoIdList: [],
      // 存储课程列表的数组
      studentSelectInfoList: [],
      // 控制表格加载状态的布尔值
      loading: false,
      // 存储总课程数量的变量
      total: 0,
    };
  },
  methods: {
    formatStatus(status) {
      const statusMap = {
        '1': '课程进行中',
        '0': '已退课',
        '2': '已结课'
      };
      return statusMap[status] || '未知状态';
    },
    // 前端排序函数
    handleSortChange({prop, order}) {
      if (!order) {
        // 如果没有排序顺序，可能是恢复到未排序状态
        this.getStudentSelectInfoList();
        return;
      }

      // 根据排序信息对 articleList 进行排序
      // articleList 是一个数组，包含要显示在表格中的文章
      this.studentSelectInfoList.sort((a, b) => {
        if (order === 'ascending') {
          return a[prop] - b[prop]; // 升序排序
        } else {
          return b[prop] - a[prop]; // 降序排序
        }
      });
    },
    // 处理搜索显示/隐藏的逻辑
    handleToggleSearch() {
      this.showSearch = !this.showSearch;
    },
    // 处理刷新数据的逻辑
    handleRefresh() {
      this.getStudentSelectInfoList();
      this.$message.success('列表已刷新');
    },
    // 搜索课程的方法
    handleQuery() {
      this.loading = true;
      // 重置当前页为第一页
      this.pagInfo.currentPage = 1;
      const pagination = {
        currentPage: this.pagInfo.currentPage,
        pageSize: this.pagInfo.pageSize
      }
      const queryParams = {
        name: this.queryParams.name,
        studentName: this.queryParams.studentName,
      }
      const DTOdata = {
        pagination,
        queryParams//只有搜索时才会带上
      }
      getStudentSelectRecordList(DTOdata).then(res => {
        if (res.data.code === 1) {
          res.data.data.list.forEach((selectCourseInfo) => {
            selectCourseInfo.selectTime = this.$common.dateFormat(selectCourseInfo.selectTime);
          });
          this.studentSelectInfoList = res.data.data.list;
          this.total = res.data.data.total;
          this.loading = false;
        } else {
          this.$message.error(res.data.msg);
        }
      })
    },
    // 处理表格行选择事件
    handleSelectionChange(selection) {
      this.studentSelectInfoIdList = selection.map(item => item.selectInfoId);
    },
    //批量删除方法
    handleBatchDelete() {
      if (this.studentSelectInfoIdList.length === 0) {
        this.$message({
          type: 'info',
          message: '请选择要删除的学生选课记录'
        });
        return;
      }
      // 确认弹窗
      this.$confirm('确定要删除选中的学生选课记录吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 这里发送删除请求
        const data = {
          ids: this.studentSelectInfoIdList,
        }
        deleteStudentSelectInfoOnList(data).then(res => {
          if (res.data.code === 1) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getStudentSelectInfoList();
          } else {
            this.$message.error(res.data.msg);
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 删除单个课程的方法
    handleDelete(id) {
      if (id) {
        this.$confirm('确定要删除该学生的选课记录吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          const data = {
            selectInfoId: id,
          }
          deleteStudentSelectInfo(data).then(res => {
            if (res.data.code === 1) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              });
              this.getStudentSelectInfoList();
            } else {
              this.$message.error(res.data.msg);
            }
          })
        }).catch(() => {
          this.$message({
            type: 'warning',
            message: '已取消删除'
          });
        });
      }
    },
    // 获取选课记录列表的方法
    async getStudentSelectInfoList() {
      this.loading = true;
      const pagination = {
        currentPage: this.pagInfo.currentPage,
        pageSize: this.pagInfo.pageSize
      }
      const queryParams = null;
      const DTOdata = {
        pagination,
        queryParams
      }
      await getStudentSelectRecordList(DTOdata).then(res => {
        if (res.data.code === 1) {
          res.data.data.list.forEach((selectInfo) => {
            selectInfo.selectTime = this.$common.dateFormat(selectInfo.selectTime);
          });
          this.studentSelectInfoList = res.data.data.list;
          this.total = res.data.data.total;
          this.loading = false;
        } else {
          this.$message.error(res.data.msg);
        }
      })
    }
  },
  created() {
    this.getStudentSelectInfoList();
  }
};
</script>

<style scoped>

.scroll-container {
  max-height: 646px; /* 您可以根据需要调整这个值 */
  overflow-y: auto;
}

.sdf {
  background: transparent;
  border-color: transparent;
  color: #76d7ff;
}

.sd {
  background: transparent;
  border-color: transparent;
  color: #fa1224;
}

.image-cover {
  width: 100px;
  height: 60px;
  border-radius: 5px;
  overflow: hidden;
  cursor: pointer;

  .image-slot {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #f5f7fa;
    color: #909399;
  }
}

.time {
  display: flex;
  align-items: center;
}
</style>

